package com.bff.gaia.streaming.api.mo.gaia.io.basiccoder;

import java.io.*;

public class StringCoder {
	String value;
	private byte[] data;

	public StringCoder() {
	}

	public StringCoder(byte[] data) {
		this.data = data;
		this.makeString();
	}

	public StringCoder(String value) {
		this.value = value;
		this.makeBytes();
	}

	private void makeString() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int flag = din.readInt();
			if (flag == -1) {
				this.value = null;
			} else {
				byte[] data = new byte[flag];
				din.readFully(data);
				this.value = new String(data);
			}
		} catch (IOException var4) {
			System.out.println("this is not the normal data");
			var4.printStackTrace();
		}

	}

	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(bos);
		if (this.value == null) {
			try {
				dataOutputStream.writeInt(-1);
			} catch (IOException var5) {
				var5.printStackTrace();
				this.data = null;
			}
		} else {
			try {
				int len = this.value.getBytes().length;
				dataOutputStream.writeInt(len);
				dataOutputStream.write(this.value.getBytes());
			} catch (IOException var4) {
				var4.printStackTrace();
				this.data = null;
			}
		}

		this.data = bos.toByteArray();
	}

	public String toString() {
		return this.value;
	}

	public String getString() {
		return this.value;
	}

	public byte[] getBytes() {
		return this.data;
	}

	public static void main(String[] args) {
	}
}
